﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using IndexMarkSilverlightClient;
using System.Collections.ObjectModel;
using IndexMarkSilverlightClient.IndexMarkServiceReference;

namespace IndexMarkSilverlightClient.Views.Controls.ReportsControls
{
    public partial class CategoryCheckListBox : UserControl, INotifyPropertyChanged
    {
        IndexMarkServiceClient client = null;

        public event EventHandler LoadCompleted;

        public void InvokeLoadCompleted(EventArgs e)
        {
            EventHandler handler = LoadCompleted;
            if (handler != null) handler(this, e);
        }



        private ObservableCollection<Category> cats = new ObservableCollection<Category>();

            public ObservableCollection<Category> Cats
            {
                get 
                {
                    return cats; 
                }
                set
                {
                    cats = value;
                    PropertyChangedInit("Cats");
                }
            }

            public IEnumerable<Category> Categories
            {
                get
                {
                    return cats.Where(x => x.Check == true).ToList();
                }
            }

        public void SelectCats(bool flag)
        {
            foreach (Category Cat in Cats)
            {
                Cat.Check = flag;
            }
            this.DataContext = Cats;
            PropertyChangedInit("Cats");
        }

        public int termID;
        
        public int TermID
        {
            get
            {
                return termID;
            }
            set
            {
                termID = value;
                LoadCats();
            }
        }
        
        public CategoryCheckListBox()
        {
            InitializeComponent();
            client = new IndexMarkServiceClient();
            client.GetCategoriesCompleted +=new EventHandler<GetCategoriesCompletedEventArgs>(client_GetCategoriesCompleted);
           // Loaded += new RoutedEventHandler(CategoryCheckListBox_Loaded);
        }

        void CategoryCheckListBox_Loaded(object sender, RoutedEventArgs e)
        {
           // LoadCats();
        }

        public void LoadCats()
        {
            client.GetCategoriesAsync(termID);
        }

        void client_GetCategoriesCompleted(object sender, GetCategoriesCompletedEventArgs e)
        {
            Cats = e.Result;
            this.DataContext = cats;
            InvokeLoadCompleted(EventArgs.Empty);
            SelectCats(true);
        }

        private void PropertyChangedInit(String PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
